ff = {'bryan' ; 'randy' ; 'edmay' ; 'gene' ; 'hoss' ; 'elaine' ; 'joe' ; 'curt';  
    'richard'; 'loch' ; 'gary' ; 'johnastin' ; ...
    'francis' ; 'carol' ; 'james'; 'zoran'; }; %'judy'; ...
  
medarray = [0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1];

for indf = 1:length(ff)
    
    cond = { 'light' 'audio' };
    for cnd = 1:length(cond)
        EEG = pop_loadset(fullfile(ff{indf}, [ ff{indf} '_' cond{cnd} '.set' ]));

        EEG2 = pop_selectevent( EEG,  'session',1, 'presentation',{ 'evoked'}, 'deleteevents', 'off', 'deleteepochs', 'on', 'invertepochs', 'off');
        pop_saveset(EEG2, fullfile(ff{indf}, [ ff{indf} '_' cond{cnd} '_evok1.set' ]));

        EEG2 = pop_selectevent( EEG,  'session',2, 'presentation',{ 'evoked'}, 'deleteevents', 'off', 'deleteepochs', 'on', 'invertepochs', 'off');
        pop_saveset(EEG2, fullfile(ff{indf}, [ ff{indf} '_' cond{cnd} '_evok2.set' ]));

        EEG2 = pop_selectevent( EEG,  'session',1, 'presentation',{ 'spontaneous'}, 'deleteevents', 'off', 'deleteepochs', 'on', 'invertepochs', 'off');
        pop_saveset(EEG2, fullfile(ff{indf}, [ ff{indf} '_' cond{cnd} '_spont1.set' ]));

        EEG2 = pop_selectevent( EEG,  'session',2, 'presentation',{ 'spontaneous'}, 'deleteevents', 'off', 'deleteepochs', 'on', 'invertepochs', 'off');
        pop_saveset(EEG2, fullfile(ff{indf}, [ ff{indf} '_' cond{cnd} '_spont2.set' ]));
    end;
    
end;

% create (EEGLAB) STUDY using all subjects
% ----------------------------------------
eeglab;
commands = {};
clear filename;
for index = 1:length(ff);
    filename{1} = fullfile(ff{index}, [ ff{index} '_audio_evok1.set' ]);
    filename{2} = fullfile(ff{index}, [ ff{index} '_audio_evok2.set' ]);
    filename{3} = fullfile(ff{index}, [ ff{index} '_audio_spont1.set' ]);
    filename{4} = fullfile(ff{index}, [ ff{index} '_audio_spont2.set' ]);
    filename{5} = fullfile(ff{index}, [ ff{index} '_light_evok1.set' ]);
    filename{6} = fullfile(ff{index}, [ ff{index} '_light_evok2.set' ]);
    filename{7} = fullfile(ff{index}, [ ff{index} '_light_spont1.set' ]);
    filename{8} = fullfile(ff{index}, [ ff{index} '_light_spont2.set' ]);
    group = 'control';
    if medarray(index), group = 'nondual'; end;
    for subindex = 1:8
        commands{end+1} = { 'index' 8*index-8+subindex 'load' filename{subindex} 'group' group };
    end;
end;
STUDY = [];
ALLEEG = [];
[STUDY ALLEEG] = std_editset( STUDY, ALLEEG,  'commands', commands);
for index = 1:length(ALLEEG)
    STUDY.datasetinfo(index).presentation = ALLEEG(index).event(1).presentation;
    STUDY.datasetinfo(index).mysession    = ALLEEG(index).event(1).session;
end;
EEG = ALLEEG;
CURRENTSTUDY = 1;
CURRENTSET = [1:length(ALLEEG)];
eeglab redraw
save -mat nondual27subjects.study STUDY


STUDY.design.setinfo(1).setindex  = [ 1 2 ];
STUDY.design.setinfo(1).subject   = 'bryan';
STUDY.design.setinfo(1).condition = 'audio';
STUDY.design.setinfo(1).group     = 'evoked';
STUDY.design.setinfo(1).trialindices = { [1:EEG(1).trials] [1:EEG(2).trials] };

STUDY.design.setinfo(2).setindex  = [ 3 4 ];
STUDY.design.setinfo(2).subject   = 'bryan';
STUDY.design.setinfo(2).condition = 'audio';
STUDY.design.setinfo(2).group     = 'spontaneous';
STUDY.design.setinfo(2).trialindices = { [1:EEG(3).trials] [1:EEG(4).trials] };

STUDY.design.setinfo(3).setindex  = [ 5 6 ];
STUDY.design.setinfo(3).subject   = 'bryan';
STUDY.design.setinfo(3).condition = 'light';
STUDY.design.setinfo(3).group     = 'evoked';
STUDY.design.setinfo(3).trialindices = { [1:EEG(5).trials] [1:EEG(6).trials] };

STUDY.design.setinfo(4).setindex  = [ 7 8 ];
STUDY.design.setinfo(4).subject   = 'bryan';
STUDY.design.setinfo(4).condition = 'light';
STUDY.design.setinfo(4).group     = 'spontaneous';
STUDY.design.setinfo(4).trialindices = { [1:EEG(7).trials] [1:EEG(8).trials] };

STUDY.design.setinfo(5).setindex  = [ 9 10 ];
STUDY.design.setinfo(5).subject   = 'randy';
STUDY.design.setinfo(5).condition = 'audio';
STUDY.design.setinfo(5).group     = 'evoked';
STUDY.design.setinfo(5).trialindices = { [1:EEG(9).trials] [1:EEG(10).trials] };

STUDY.design.setinfo(6).setindex  = [ 11 12 ];
STUDY.design.setinfo(6).subject   = 'randy';
STUDY.design.setinfo(6).condition = 'audio';
STUDY.design.setinfo(6).group     = 'spontaneous';
STUDY.design.setinfo(6).trialindices = { [1:EEG(11).trials] [1:EEG(12).trials] };

STUDY.design.setinfo(7).setindex  = [ 13 14 ];
STUDY.design.setinfo(7).subject   = 'randy';
STUDY.design.setinfo(7).condition = 'light';
STUDY.design.setinfo(7).group     = 'evoked';
STUDY.design.setinfo(7).trialindices = { [1:EEG(13).trials] [1:EEG(14).trials] };

STUDY.design.setinfo(8).setindex  = [ 15 16 ];
STUDY.design.setinfo(8).subject   = 'randy';
STUDY.design.setinfo(8).condition = 'light';
STUDY.design.setinfo(8).group     = 'spontaneous';
STUDY.design.setinfo(8).trialindices = { [1:EEG(15).trials] [1:EEG(16).trials] };

for index = 1: length(STUDY.design.setinfo)
    dat      = STUDY.design.setinfo(index).setindex;
    STUDY.design.setinfo(index).filebase = fullfile(ALLEEG(dat(1)).filepath, [ 'design1_' subject '_' cond '_' group ]);
    cond     = STUDY.design.setinfo(index).condition;
    subject  = STUDY.design.setinfo(index).subject;
    group    = STUDY.design.setinfo(index).group;
    trials   = STUDY.design.setinfo(index).trialindices;
    filebase = STUDY.design.setinfo(index).filebase;
    std_erp(ALLEEG(dat), 'channels', { ALLEEG(1).chanlocs.labels }, 'trialindices', trials, 'fileout', filebase);
end;
STUDY.design.condition = unique( { STUDY.design.setinfo.condition });
STUDY.design.group     = unique( { STUDY.design.setinfo.group     });
STUDY.currentdesign    = 1;

% remake setinds and allinds
% --------------------------
STUDY = std_changroup2(STUDY, ALLEEG, [], 'on');
STUDY = std_erpplot(STUDY,ALLEEG,'channels',{ 'E1' }, 'design', 1);


STUDY = std_makedesign(STUDY, ALLEEG, 2, 'factor1', 'condition', 'factor2', 'presentation', 'selectsubject', { 'group' 'nondual' });
STUDY = std_makedesign(STUDY, ALLEEG, 2, 'factor1', 'condition', 'factor2', 'presentation', 'selectsubject', { 'group' 'nondual' });

STUDY = std_makedesign(STUDY, ALLEEG, 3, 'factor1', 'condition', 'factor2', 'mysession', 'selectsubject', { 'group' 'nondual' });
